/**
 * author: Hanflyee  flyee1980@163.com
 * vision: v1.0
 * data: 2009-8-11
 */
package com.flyee.controller.timer{
	
	import com.flyee.controller.tools.cartoon.model.AppearanceConfig;
	import com.flyee.model.Charactor;
	import com.flyee.service.pool.ObjectCollection;
	import com.flyee.utils.gs.TweenMax;
	import com.flyee.viewer.CharactorViewer;
	import com.flyee.viewer.SkillViewer;
	
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	public class ShootSkillTimer extends Timer{
		
		//施法时间, 毫秒
		public var enchantTime : int = 400;
		//命中后特效时间
		public var afterHitTargetTime : int = 300;
		//飞行时间
		private var flyTime : Number = 0;
		//飞行速度，象素/S
		private var speed : Number = 700;		
		
		//释放技能者
		public var playerViewer : CharactorViewer;
		//目标对象
		public var targetViewer : CharactorViewer;
		//技能效果
		public var skillViewer : SkillViewer;
		//效果处理链
		private var arr : Array = new Array();
		//目标
		public var target : Charactor;
		//伤害
		public var damage : int;
		
		//动画结束后的回调
		public var callbackFn : Function;
		
		public function ShootSkillTimer(delay:Number, repeatCount:int=0){
			super(delay, repeatCount);
			this.addEventListener(TimerEvent.TIMER, timerHandler);
			arr.push(this.enchantHandler);
			arr.push(this.flyHandler);
			arr.push(this.hitTargetHandler);
		}

		private function timerHandler(event : TimerEvent) : void {
			if(arr.length == 0){
				this.finish();
				return ;
			}
			var handler : Function = arr[0] as Function;
			handler.call(null);
			
		}
		
		/**施法阶段*/
		private function enchantHandler() : void {
			if(currentCount*delay >= enchantTime){
				arr.shift();
				//人物施法状态设为false
				var player : Charactor = ObjectCollection.getCharacterByVid(playerViewer.vid);
				player.skillStatus = false;
				skillViewer.visible = true;
				//人物动画设为准备中
				var ct : CartoonTimer = ObjectCollection.cartoonTimerMap.get(playerViewer.vid) as CartoonTimer;
				ct.setCurrentStatus(AppearanceConfig.MOVE_BY_READY, ct.getDirect(), 0);
				skillViewer.cartoon.gotoAndStop("FLY");
				//待完成，隐藏进度条， 
				//缓动代替时间位移
				var tarX : Number = targetViewer.x + targetViewer.width / 2;
				var tarY : Number = targetViewer.y + targetViewer.height / 2;
				var skiX : Number = skillViewer.x + skillViewer.width / 2;
				var skiY : Number = skillViewer.y + skillViewer.height / 2;
				var _dx : Number = tarX - skiX;
				var _dy : Number = tarY - skiY;
				var distance : Number = Math.sqrt(_dx*_dx + _dy*_dy); 
				flyTime = distance / speed ;
				TweenMax.to(skillViewer, flyTime, {x:tarX-skillViewer.width/2-10, y:tarY-skillViewer.height/2-40, onComplete:flyHandler, onCompleteParams:[true]});
			}else{
				//推进进度条
			}
		}
		
		/**魔法飞行阶段*/
		private function flyHandler(b : Boolean = false) : void {
//			var tarX : Number = targetViewer.x + targetViewer.width / 2;
//			var tarY : Number = targetViewer.y + targetViewer.height / 2;
//			var skiX : Number = skillViewer.x + skillViewer.width / 2;
//			var skiY : Number = skillViewer.y + skillViewer.height / 2;
//			var _dx : Number = tarX - skiX;
//			var _dy : Number = tarY - skiY;
//			if(Math.abs(_dx)<10 && Math.abs(_dy)<10){
			if(!b) return;//
				skillViewer.cartoon.gotoAndPlay("HIT");
				arr.shift();
				reset();
				start();
//			}else{
//				
//				var distance : Number = Math.sqrt(_dx*_dx + _dy*_dy); 
//				var dx : Number = _dx * 5 / distance;
//				var dy : Number = _dy * 5 / distance;
//				skillViewer.x += dx;
//				skillViewer.y += dy;
//			}
			
		}
		
		/**命中目标阶段*/
		private function hitTargetHandler() : void {
			skillViewer.hitTarget();
			if(currentCount * delay >= afterHitTargetTime){
				ObjectCollection.currentSceneViewer.driftLayer.removeChild(skillViewer);			
				arr.shift();	
			}
		}
		
		/**结束技能动画*/
		private function finish() : void {
			//执行回调
			callbackFn(ObjectCollection.getCharacterByVid(playerViewer.vid), target, damage);
			this.stop();
			this.removeEventListener(TimerEvent.TIMER, timerHandler);
			playerViewer = null;
			targetViewer = null;
			skillViewer.dispose();
			skillViewer = null;
			target = null;
			callbackFn = null;
		}
	}
}